﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_MembershipProvider_Access_Controls_EditUser : System.Web.UI.UserControl
{
    string username;

    MembershipUser user;

    Object _dataItem;
    public object DataItem
    {
        get
        {
            return this._dataItem;
        }
        set
        {
            this._dataItem = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        username = txtUserName.Text;
        user = Membership.GetUser(username);
    }
    private void Page_PreRender()
    {
        // Load the User Roles into checkboxes.
        UserRoles.DataSource = System.Web.Security.Roles.GetAllRoles();
        UserRoles.DataBind();

        // Bind these checkboxes to the User's own set of roles.
        string[] userRoles = System.Web.Security.Roles.GetRolesForUser(txtUserName.Text);
        foreach (string role in userRoles)
        {
            ListItem checkbox = UserRoles.Items.FindByValue(role);
            checkbox.Selected = true;
        }
    }
    protected void checkBoxCustomer_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //Need to handle the update manually because MembershipUser does not have a
        //parameterless constructor  

        user.Email = txtEmail.Text.Trim();
        user.Comment = txtComment.Text.Trim();
        user.IsApproved = (bool)chkActiveUser.Checked;
        try
        {
            // Update user info:
            Membership.UpdateUser(user);

            // Update user roles:
            UpdateUserRoles();

        }
        catch (Exception ex)
        {
            //UserUpdateMessage.Text = "Update Failed: " + ex.Message;

            //e.Cancel = true;
            //UserInfo.ChangeMode(DetailsViewMode.ReadOnly);
        }

    }

    private void UpdateUserRoles()
    {
        foreach (ListItem rolebox in UserRoles.Items)
        {
            if (rolebox.Selected)
            {
                if (!System.Web.Security.Roles.IsUserInRole(username, rolebox.Text))
                {
                    System.Web.Security.Roles.AddUserToRole(username, rolebox.Text);
                }
            }
            else
            {
                if (System.Web.Security.Roles.IsUserInRole(username, rolebox.Text))
                {
                    System.Web.Security.Roles.RemoveUserFromRole(username, rolebox.Text);
                }
            }
        }
    }

    private void DeleteUser(object sender, EventArgs e)
    {
        //Membership.DeleteUser(username, false); // DC: My apps will NEVER delete the related data.
        Membership.DeleteUser(username, true); // DC: except during testing, of course!
        Response.Redirect("users.aspx");
    }

    private void UnlockUser(object sender, EventArgs e)
    {
        // Dan Clem, added 5/30/2007 post-live upgrade.

        // Unlock the user.
        user.UnlockUser();

        // DataBind the GridView to reflect same.
        //UserInfo.DataBind();
    }

    protected void btnUnlockUser_Click(object sender, EventArgs e)
    {
        // Dan Clem, added 5/30/2007 post-live upgrade.

        // Unlock the user.
        user.UnlockUser();
        // DataBind the GridView to reflect same.
        //UserInfo.DataBind();
    }

    protected void btnDeleteUser_Click(object sender, EventArgs e)
    {
        //Membership.DeleteUser(username, false); // DC: My apps will NEVER delete the related data.
        Membership.DeleteUser(username, true); // DC: except during testing, of course!
        //Response.Redirect("users.aspx");
    }

    protected void UserRoles_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (ListItem checkItem in UserRoles.Items)
        {
            if (checkItem.Text == "Customers")
            {

                if (checkItem.Selected)
                {
                    //uxCustomerDetails.Visible = true;
                }
                else
                {
                    //uxCustomerDetails.Visible = false;
                }

            }
        }
    }
}
